﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DoAn_BumTruong_DesignPattern.Admin
{
    public partial class EditMenu : System.Web.UI.Page
    {
        Facades facadeMenu = new Facades();

        MenuController mnController = new MenuController();
        Menus menu = new Menus();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btClose.Click += btClose_Click;
            btReset.Click += btReset_Click;
            btSave.Click += btSave_Click;
        }

        void btSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Menus m = GetData();
                if (m.MenuID > 0)
                {
                    bool status = facadeMenu.UpdateMenu(m);
                    if (status)
                        SetData(m);
                    lblStatus.Text = status ? "Update successful" : "Update failed";
                }
                else
                {
                    bool status = facadeMenu.AddMenu(m);
                    lblStatus.Text = status ? "Insert successful" : "Insert failed";
                }
            }
        }

        void btReset_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtAlias.Text = "";
            CKEditorDescription.Text = "";
            CheckBoxActive.Checked = false;
            txtOrderNo.Text = "";
            DropDownListParent.SelectedIndex = 0;
            DropDownListMenuType.SelectedIndex = 0;
        }

        void btClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Menu.aspx");
        }

        //Load
        private void LoadData()
        {
            lblId.Text = Request.QueryString["MenuID"] ?? "";
            if (lblId.Text != "")
            {
                lblTitle.Text = "Edit Menu";
                menu = mnController.Single(lblId.Text);
                SetData(menu);
            }
            else
            {
                lblTitle.Text = "Add Menu";
                LoadParent();
                LoadMenuType();
            }
        }

        //GetData
        public Menus GetData()
        {
            menu = null;
            if (lblId.Text != "")
            {
                menu = mnController.Single(lblId.Text);
            }
            else
            {
                menu = new Menus();
            }
            menu.Name = txtName.Text;
            menu.Alias = txtAlias.Text;
            menu.Descriptions = CKEditorDescription.Text;
            if (txtOrderNo.Text != "")
                menu.OrderNo = int.Parse(txtOrderNo.Text);
            else
                menu.OrderNo = 0;
            menu.MenuType = int.Parse(DropDownListMenuType.SelectedValue);
            menu.ParentID = int.Parse(DropDownListParent.SelectedValue);
            menu.Active = CheckBoxActive.Checked;
            return menu;
        }

        //SetData
        private void SetData(Menus m)
        {
            lblId.Text = menu.MenuID.ToString();
            txtName.Text = menu.Name;
            txtAlias.Text = menu.Alias;
            CKEditorDescription.Text = menu.Descriptions;
            txtOrderNo.Text = menu.OrderNo.ToString();
            CheckBoxActive.Checked = menu.Active;
            LoadParent();
            DropDownListParent.SelectedValue = menu.ParentID.ToString() ?? "";
            LoadMenuType();
            DropDownListMenuType.SelectedValue = menu.MenuType.ToString();
        }

        //MenuType
        private void LoadMenuType()
        {
            int i = 0;
            foreach (string item in Enum.GetNames(typeof(MenuTypes)))
            {
                DropDownListMenuType.Items.Add(new ListItem(item, i.ToString()));
                i++;
            }
        }

        //Parent: Article
        private void LoadParent()
        {
            DropDownListParent.DataSource = mnController.SelectAll();
            DropDownListParent.DataTextField = "Name";
            DropDownListParent.DataValueField = "MenuID";
            DropDownListParent.DataBind();
        }
    }
}